﻿package game.breeze.message.codec;

import game.breeze.message.AppMessage;
import game.breeze.message.Message;
import game.breeze.socket.util.BufferWrapper;

/**
 * 
 * Class desc there
 * 
 * @author yangwei ywengineer@gmail.com
 * 
 */
public class AppMessageDecoder implements IMessageDecoder<Message, BufferWrapper> {

	@Override
	public Message decode(BufferWrapper buffer) {
		if (buffer.remaining() < 8)
			return null;
		int position = buffer.position();

		int type = buffer.readInt();
		int length = buffer.readInt();

		if (buffer.remaining() < length) {
			buffer.position(position);
			return null;
		}

		AppMessage message = AppMessage.createMessage(type, length);

		message.getBuffer().writeBytes(buffer.get(length));
		return message;
	}
}